import router from "next/router";
import { getEncryptedParam, getToken } from "./auth";
import { getSignAll, parseParams } from "./authApp"

const whiteList = ['/v4/login/phoneValidCode','/v4/user/userInfoAlls','/v4/visitor/userInfo','/v4/login/phoneLogin']
export const fetchCommon: (url: string, options: { credentials: 'include'|'omit', method?: string;body?:FormData }) => Promise<Response> = (url, options) => { 
    
    const isH5 = router.asPath.includes('/h5')
    let sinUrl = url
    const whiteUrl = whiteList.find(v=>sinUrl.includes(v))
    if (whiteUrl) { 
        sinUrl = whiteUrl
    }
    sinUrl = sinUrl.split('?')[0]
    let authQuery = ''
    if (isH5) {
        const authH5 = { ...getSignAll(options.method || 'GET', sinUrl), pt: 'h5', needLogin: false }
        authQuery = parseParams(authH5)
    } else { 
        const encryptionObj = getEncryptedParam(getToken(), {})
        authQuery = encryptionObj.param
    }
    return fetch(url+(url.includes('?')?'&':'?')+authQuery,options)
}
